﻿
using System.Linq.Expressions;
using AutoMapper;
using CozeDomain.Entities;
using CozeDomain.IRepositories;
using CozeDomain.IServices;
using D.Util.UtilCache;
using D.UtilCore.Dtos;
using D.UtilCore.JwtUtil; 
using D.UtilFreesqlOrm;
using Microsoft.AspNetCore.Http;
using D.Util.UtilNLog;
using CozeDto.ProjectDtos;
using CozeDto.UserDtos;
using CozeDto.MyCollectionDtos;

namespace CozeDomain.Services
{
    /// <summary>
    /// 管理员业务实现
    /// </summary>
    public class MyCollectionEntityService : BaseService<MyCollectionEntity>, IMyCollectionEntityService
    {
        private readonly IMapper _mapper;
        private readonly ICacheService _cache;
        private readonly IJwtService _tokenService;
        private readonly INLogService _nlogService;
        private readonly IMyCollectionEntityRepository _repository;
        private readonly IHttpContextAccessor _httpContextAccessor;

        /// <summary>
        /// 构造注入
        /// </summary>
        public MyCollectionEntityService(IMapper mapper, ICacheService cache, IJwtService tokenService, INLogService nlogService, IHttpContextAccessor httpContextAccessor,
            IMyCollectionEntityRepository repository)
        {
            // 用于base方法
            base._baseDal = repository;
            this._repository = repository;
            _cache = cache;
            _mapper = mapper;
            _nlogService = nlogService;
            _tokenService = tokenService;
            _httpContextAccessor = httpContextAccessor;
        }

        #region 增删改查

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="reqModel"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddMyCollectionAsync(ReqAddMyCollection reqModel)
        {
            var insertEntity = _mapper.Map<ReqAddMyCollection, MyCollectionEntity>(reqModel);
            insertEntity.DeleteFlag = false;
            insertEntity.CreateTime = DateTime.Now;
            insertEntity.Id = D.UtilFreesqlOrm.FreesqlOrm.NewId;

            var dbres = await _repository.AddAsync(insertEntity);

            return dbres.NormalResponse();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> DeleteMyCollectionAsync(List<string> ids)
        {
            var dbres = await _repository.DeleteAsync(m => ids.Contains(m.Id));
            return dbres.NormalResponse();
        }

        /// <summary>
        /// 获取分页
        /// </summary>
        /// <param name="reqQueryProjectPage"></param>
        /// <returns></returns>
        public async Task<ApiResult<PageModel<RepMyCollectionInfo>>> QueryMyCollectionPagesAsync(ReqQueryMyCollectionPage reqQueryMyCollectionPage)
        {
            Expression<Func<MyCollectionEntity, bool>> where = m=>m.UserId == reqQueryMyCollectionPage.UserId;

            var dbres = await _repository.GetPagesAsync(reqQueryMyCollectionPage.PageSize, reqQueryMyCollectionPage.PageNo, where, m => m.CreateTime);

            var resData = _mapper.Map<List<MyCollectionEntity>, List<RepMyCollectionInfo>>(dbres.Data);
            var res = new PageModel<RepMyCollectionInfo>()
            {
                PageNo = dbres.PageNo,
                PageSize = dbres.PageSize,
                TotalCount = dbres.TotalCount,
                TotalPage = dbres.TotalPage,
                Data = resData
            };

            return res.GetResponse();
        }
        #endregion
    }
}